package galaxy.generator;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * A composite structure where each node has a String value and 0-n children
 */
public class Node {
	private String value;
	private List<Node> children = new LinkedList<Node>();
	
	public static List<Node> createList(String values)
	{
		List<Node> symbols = new LinkedList<Node>();
		for (int i = 0; i < values.length(); i++) {
			symbols.add(new Node(values.substring(i, i+1)));
		}
		return symbols;
	}
	
	public Node(String value)
	{
		switch(value.length()) {
			case 0:
			case 1:
				this.value = value;
			break;
			default:
				this.value = "";
				addChildren(createList(value));
		}
	}
	
	public String getValue()
	{
		return value;
	}
	
	public boolean hasChildren()
	{
		return children.size() > 0;
	}
	
	public List<Node> getChildren()
	{
		return children;
	}
	
	public Node addChild(Node child)
	{
		children.add(child);
		return this;
	}
	
	public Node addChildren(Collection<Node> children)
	{
		this.children.addAll(children);
		return this;
	}
	
	public Node accept(NodeVisitor w)
	{
		w.visit(this);
		return this;
	}
}
